﻿using System;
using System.Linq;
using Hos.ScheduleMaster.Core;
using Hos.ScheduleMaster.Core.Common;
using Hos.ScheduleMaster.Core.Interface;
using Hos.ScheduleMaster.Core.Models;
using Hos.ScheduleMaster.Core.Services;
using Hos.ScheduleMaster.Web.Extension;
using Hos.ScheduleMaster.Web.Filters;
using Microsoft.AspNetCore.Mvc;

namespace Hos.ScheduleMaster.Web.Controllers
{
    [Route("/[controller]/[action]")]
    public class GroupController : AdminController
    {

        [Autowired]
        public IScheduleGroupService _scheduleGroupService { get; set; }
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 异步查询用户分页数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        [AjaxRequestOnlyAttribute]
        public ActionResult LoadScheduleGroupPager(string name)
        {
            var pager = new ListPager<ScheduleGroupEntity>(PageIndex, PageSize);
            if (!string.IsNullOrEmpty(name))
            {
                pager.AddFilter(m => m.GroupName.Contains(name) || m.Remark.Contains(name));
            }
            pager = _scheduleGroupService.GetScheduleGroupPager(pager);
            return GridData(pager.Total, pager.Rows.Select(x => new
            {
                CreateTime = x.CreateTime.StringFormat(),
                x.Id,
                x.GroupName,
                x.Remark,
                x.CreateUserId,
                x.CreateUserName
            }));
        }

        /// <summary>
        /// 编辑页面
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id = "")
        {
            if (!string.IsNullOrWhiteSpace(id))
            {
                var scheduleGroup = _scheduleGroupService.GetScheduleGroupById(id);
                if (scheduleGroup == null)
                {
                    return PageNotFound();
                }
                return View(scheduleGroup);
            }
            return View();
        }

        /// <summary>
        /// 保存页面的编辑信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, AjaxRequestOnlyAttribute]
        public ActionResult Edit(ScheduleGroupEntity model)
        {
            if (ModelState.IsValid)
            {
                model.CreateUserId = CurrentAdmin?.Id ?? 0;
                model.CreateUserName = CurrentAdmin?.UserName ?? "";
                if (!string.IsNullOrWhiteSpace(model.Id))
                {
                    var result = _scheduleGroupService.EditScheduleGroup(model);
                    if (result)
                    {
                        return SuccessTip("编辑分组成功！", Url.Action("Index"));
                    }
                    return DangerTip("编辑分组失败！");
                }
                else
                {
                    if (!_scheduleGroupService.CheckScheduleGroupName(model.GroupName, model.Id))
                    {
                        return DangerTip("分组名已存在！");
                    }
                    var result = _scheduleGroupService.AddScheduleGroup(model);
                    if (result)
                    {
                        return SuccessTip("创建分组成功！", Url.Action("Index"));
                    }
                    return DangerTip("创建分组失败！");
                }
            }
            return DangerTip("数据验证失败！");
        }

        /// <summary>
        /// 删除账号
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, AjaxRequestOnlyAttribute]
        public ActionResult Delete(string id)
        {
            if (_scheduleGroupService.CanDelete(id))
            {
                if (_scheduleGroupService.DeleteGroupd(id))
                {
                    return this.JsonNet(true, "删除成功！");
                }
                return this.JsonNet(false, "删除失败");
            }
            else
            {
                return this.JsonNet(false, "该分组存在任务，无法删除");
            }

        }
    }
}
